//
//  HPTask.m
//
//  Created by 啊  on 16/4/24
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "HPTask.h"


NSString *const kHPTaskTRemark = @"tRemark";
NSString *const kHPTaskTTitle = @"tTitle";
NSString *const kHPTaskIsDone = @"isDone";
NSString *const kHPTaskTPublishUser = @"tPublishUser";
NSString *const kHPTaskCreateUser = @"createUser";
NSString *const kHPTaskCreateTime = @"createTime";
NSString *const kHPTaskTMaxAge = @"tMaxAge";
NSString *const kHPTaskTDescribe = @"tDescribe";
NSString *const kHPTaskTMinAge = @"tMinAge";
NSString *const kHPTaskTId = @"tId";
NSString *const kHPTaskTAuthor = @"tAuthor";
NSString *const kHPTaskTType = @"tType";
NSString *const kHPTaskTPublishDate = @"tPublishDate";


@interface HPTask ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation HPTask

@synthesize tRemark = _tRemark;
@synthesize tTitle = _tTitle;
@synthesize isDone = _isDone;
@synthesize tPublishUser = _tPublishUser;
@synthesize createUser = _createUser;
@synthesize createTime = _createTime;
@synthesize tMaxAge = _tMaxAge;
@synthesize tDescribe = _tDescribe;
@synthesize tMinAge = _tMinAge;
@synthesize tId = _tId;
@synthesize tAuthor = _tAuthor;
@synthesize tType = _tType;
@synthesize tPublishDate = _tPublishDate;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.tRemark = [self objectOrNilForKey:kHPTaskTRemark fromDictionary:dict];
            self.tTitle = [self objectOrNilForKey:kHPTaskTTitle fromDictionary:dict];
            self.isDone = [[self objectOrNilForKey:kHPTaskIsDone fromDictionary:dict] doubleValue];
            self.tPublishUser = [self objectOrNilForKey:kHPTaskTPublishUser fromDictionary:dict];
            self.createUser = [self objectOrNilForKey:kHPTaskCreateUser fromDictionary:dict];
            self.createTime = [self objectOrNilForKey:kHPTaskCreateTime fromDictionary:dict];
            self.tMaxAge = [[self objectOrNilForKey:kHPTaskTMaxAge fromDictionary:dict] doubleValue];
            self.tDescribe = [self objectOrNilForKey:kHPTaskTDescribe fromDictionary:dict];
            self.tMinAge = [[self objectOrNilForKey:kHPTaskTMinAge fromDictionary:dict] doubleValue];
            self.tId = [[self objectOrNilForKey:kHPTaskTId fromDictionary:dict] doubleValue];
            self.tAuthor = [self objectOrNilForKey:kHPTaskTAuthor fromDictionary:dict];
            self.tType = [self objectOrNilForKey:kHPTaskTType fromDictionary:dict];
            self.tPublishDate = [self objectOrNilForKey:kHPTaskTPublishDate fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.tRemark forKey:kHPTaskTRemark];
    [mutableDict setValue:self.tTitle forKey:kHPTaskTTitle];
    [mutableDict setValue:[NSNumber numberWithDouble:self.isDone] forKey:kHPTaskIsDone];
    [mutableDict setValue:self.tPublishUser forKey:kHPTaskTPublishUser];
    [mutableDict setValue:self.createUser forKey:kHPTaskCreateUser];
    [mutableDict setValue:self.createTime forKey:kHPTaskCreateTime];
    [mutableDict setValue:[NSNumber numberWithDouble:self.tMaxAge] forKey:kHPTaskTMaxAge];
    [mutableDict setValue:self.tDescribe forKey:kHPTaskTDescribe];
    [mutableDict setValue:[NSNumber numberWithDouble:self.tMinAge] forKey:kHPTaskTMinAge];
    [mutableDict setValue:[NSNumber numberWithDouble:self.tId] forKey:kHPTaskTId];
    [mutableDict setValue:self.tAuthor forKey:kHPTaskTAuthor];
    [mutableDict setValue:self.tType forKey:kHPTaskTType];
    [mutableDict setValue:self.tPublishDate forKey:kHPTaskTPublishDate];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.tRemark = [aDecoder decodeObjectForKey:kHPTaskTRemark];
    self.tTitle = [aDecoder decodeObjectForKey:kHPTaskTTitle];
    self.isDone = [aDecoder decodeDoubleForKey:kHPTaskIsDone];
    self.tPublishUser = [aDecoder decodeObjectForKey:kHPTaskTPublishUser];
    self.createUser = [aDecoder decodeObjectForKey:kHPTaskCreateUser];
    self.createTime = [aDecoder decodeObjectForKey:kHPTaskCreateTime];
    self.tMaxAge = [aDecoder decodeDoubleForKey:kHPTaskTMaxAge];
    self.tDescribe = [aDecoder decodeObjectForKey:kHPTaskTDescribe];
    self.tMinAge = [aDecoder decodeDoubleForKey:kHPTaskTMinAge];
    self.tId = [aDecoder decodeDoubleForKey:kHPTaskTId];
    self.tAuthor = [aDecoder decodeObjectForKey:kHPTaskTAuthor];
    self.tType = [aDecoder decodeObjectForKey:kHPTaskTType];
    self.tPublishDate = [aDecoder decodeObjectForKey:kHPTaskTPublishDate];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_tRemark forKey:kHPTaskTRemark];
    [aCoder encodeObject:_tTitle forKey:kHPTaskTTitle];
    [aCoder encodeDouble:_isDone forKey:kHPTaskIsDone];
    [aCoder encodeObject:_tPublishUser forKey:kHPTaskTPublishUser];
    [aCoder encodeObject:_createUser forKey:kHPTaskCreateUser];
    [aCoder encodeObject:_createTime forKey:kHPTaskCreateTime];
    [aCoder encodeDouble:_tMaxAge forKey:kHPTaskTMaxAge];
    [aCoder encodeObject:_tDescribe forKey:kHPTaskTDescribe];
    [aCoder encodeDouble:_tMinAge forKey:kHPTaskTMinAge];
    [aCoder encodeDouble:_tId forKey:kHPTaskTId];
    [aCoder encodeObject:_tAuthor forKey:kHPTaskTAuthor];
    [aCoder encodeObject:_tType forKey:kHPTaskTType];
    [aCoder encodeObject:_tPublishDate forKey:kHPTaskTPublishDate];
}

- (id)copyWithZone:(NSZone *)zone
{
    HPTask *copy = [[HPTask alloc] init];
    
    if (copy) {

        copy.tRemark = [self.tRemark copyWithZone:zone];
        copy.tTitle = [self.tTitle copyWithZone:zone];
        copy.isDone = self.isDone;
        copy.tPublishUser = [self.tPublishUser copyWithZone:zone];
        copy.createUser = [self.createUser copyWithZone:zone];
        copy.createTime = [self.createTime copyWithZone:zone];
        copy.tMaxAge = self.tMaxAge;
        copy.tDescribe = [self.tDescribe copyWithZone:zone];
        copy.tMinAge = self.tMinAge;
        copy.tId = self.tId;
        copy.tAuthor = [self.tAuthor copyWithZone:zone];
        copy.tType = [self.tType copyWithZone:zone];
        copy.tPublishDate = [self.tPublishDate copyWithZone:zone];
    }
    
    return copy;
}


@end
